package com.itlangzi.mybatis.dialect;

import com.alibaba.druid.util.JdbcConstants;
import com.itlangzi.mybatis.dialect.helper.DialectEnum;
import com.itlangzi.mybatis.dialect.helper.DialectHelper;

/**
 * MySQL 分页方言
 */
public class MySQLDialect implements Dialect {

	@Override
	public boolean supportsPage() {
		return true;
	}

	@Override
	public String buildCountSQL(String originalSQL) {
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append("SELECT COUNT(1) AS TOTAL FROM ( ").append(DialectHelper.getNoneOrderBySQL(originalSQL, JdbcConstants.MYSQL)).append(" ) ");
		return sqlBuilder.toString();
	}

	@Override
	public String buildPageSQL(String originalSQL, Integer page, Integer size) {
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(originalSQL);
		sqlBuilder.append(" LIMIT ");
		sqlBuilder.append((page - 1) * size);
		sqlBuilder.append(" , ");
		sqlBuilder.append(page * size);
		return sqlBuilder.toString();
	}

	@Override
	public String getDialectName() {
		return DialectEnum.MYSQL.getDbName();
	}

}
